package cc.realxyq.demo.aware;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StringValueResolver;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.atomic.LongAdder;

/**
 * Bean 对象 生命周期的 演示
 * @author kwhan
 * @date 2022年01月12日 14:23
 */
@Slf4j
@Getter
public class DemoAware implements BeanNameAware, BeanFactoryAware, BeanClassLoaderAware, ApplicationContextAware,
        EnvironmentAware, EmbeddedValueResolverAware, ResourceLoaderAware, ApplicationEventPublisherAware,
        MessageSourceAware, InitializingBean, DisposableBean {
    private final LongAdder longAdder;

    public DemoAware() {
        longAdder = new LongAdder();
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), classLoader.getClass().getSimpleName());
    }

    private void logInfo(String methodName, Object object) {
        longAdder.increment();
        log.info("步骤{}，DemoAware.{} 方法执行了。。。 {}", longAdder, methodName, object);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), beanFactory.getClass().getSimpleName());
    }

    @Override
    public void setBeanName(String name) {

        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), name);

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), applicationContext.getClass().getSimpleName());
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {

        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), resolver.getClass().getSimpleName());
    }

    @Override
    public void setEnvironment(Environment environment) {

        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), environment.getClass().getSimpleName());
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {

        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), applicationEventPublisher.getClass().getSimpleName());
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {

        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), resourceLoader.getClass().getSimpleName());
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {

        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), messageSource.getClass().getSimpleName());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), null);
    }

    @Override
    public void destroy() throws Exception {
        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), null);
    }

    public void init() {
        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), null);
    }

    public void close() {
        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), null);
    }

    @PostConstruct
    public void postConstruct() {
        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), null);
    }

    @PreDestroy
    public void preDestroy() {
        logInfo(Thread.currentThread().getStackTrace()[1].getMethodName(), null);
    }

}
